// SPDX-License-Identifier: MPL-2.0
// (c) Hare authors <https://harelang.org>

use bytes;
use crypto::bigint::*;

let pubbuf: [PUBEXP_BUFSZ]u8 = [0...];
let privbuf: [PRIVEXP_BUFSZ]u8 = [0...];
let pkcs1_verifybuf: [PKCS1_VERIFYBUFSZ]u8 = [0...];
let pkcs1_signbuf: [PKCS1_SIGNBUFSZ]u8 = [0...];

@test fn tiny() void = {
	let pub = pubparams {
		e = [0x01, 0x00, 0x01],
		n = [0x1, 0x87],
		...
	};

	let msg: []u8 = [0x00, 0xc];

	pubexp(&pub, msg, pubbuf)!;
	assert(bytes::equal(msg, [0x01, 0x2d]));
};

@test fn smallprivexp() void = {
	let priv = privparams {
		nbitlen = 200,
		p = [
			0x0a, 0xd2, 0xbe, 0xc0, 0xbf, 0x9a, 0xfa, 0x3b, 0x64,
			0x7a, 0x27, 0x33, 0x59,
		],
		q = [
			0x0c, 0xfd, 0x9a, 0xec, 0x42, 0xcd, 0xce, 0xc0, 0xc5,
			0x43, 0x31, 0xbf, 0x33,
		],
		dp = [
			0x03, 0x3c, 0x37, 0xb4, 0xda, 0x11, 0x73, 0x57, 0x29,
			0x93, 0xb2, 0x00, 0x11,
		],
		dq = [
			0x01, 0xde, 0xde, 0xd9, 0x18, 0x81, 0x84, 0x3d, 0x13,
			0xea, 0xaa, 0x16, 0x2b,
		],
		iq = [
			0x08, 0x24, 0xbc, 0x1a, 0xea, 0xcc, 0xdf, 0xe1, 0x5a,
			0x0a, 0x6f, 0x32, 0xa8,
		],
		...
	};

	let x: [_]u8 = [
		0x0c, 0x5b, 0xea, 0x82, 0x38, 0xc2, 0xfc, 0x7a, 0x2c, 0xe3,
		0xc1, 0x39, 0x44, 0x5b, 0x21, 0xc2, 0xa4, 0x6e, 0xb1, 0x7b,
		0xeb, 0xbb, 0xd4, 0xea, 0xfc,
	];

	const expected: [_]u8 = [
		0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
		0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
		0x00, 0x00, 0xcc, 0x07, 0xc9,
	];

	privexp(&priv, x, privbuf)!;

	assert(bytes::equal(expected, x));
};

@test fn pubprivexp() void = {
	const x: [_]u8 = [
		0x6e, 0xb5, 0x5f, 0xbd, 0x48, 0x09, 0x08, 0x7c, 0x01, 0xb3,
		0x74, 0x26, 0x73, 0x9d, 0x3e, 0xd5, 0x08, 0x7d, 0xe8, 0x11,
		0x01, 0x21, 0x31, 0x11, 0x91, 0x34, 0x5b, 0xe5, 0x6c, 0x37,
		0x79, 0x7b, 0xdb, 0x75, 0x16, 0x62, 0x0e, 0x7d, 0x1d, 0xd1,
		0x04, 0x45, 0xbc, 0xa9, 0x79, 0xd0, 0xb1, 0x1d, 0x1e, 0x20,
		0x65, 0x37, 0x92, 0x90, 0xa8, 0xd3, 0x5f, 0x07, 0x24, 0x54,
		0x53, 0x97, 0x69, 0x84, 0xe2, 0xbb, 0xc0, 0xb5, 0x82, 0x4f,
		0x29, 0xac, 0xc9, 0x07, 0xa7, 0x75, 0x08, 0x1c, 0x0c, 0x72,
		0x9f, 0x35, 0x1b, 0x75, 0xb2, 0x79, 0x3f, 0x41, 0xa5, 0xcb,
		0x9d, 0x69, 0x02, 0xa8, 0x08, 0xfe, 0x11, 0x19, 0x2f, 0xc2,
		0xdb, 0x0e, 0xa6, 0xe0, 0xc4, 0x44, 0x33, 0xd6, 0xad, 0x59,
		0x11, 0xa7, 0x38, 0xc0, 0xe7, 0x37, 0x21, 0xa8, 0x13, 0x96,
		0xe9, 0x63, 0x25, 0xd9, 0x2e, 0xbf, 0x10, 0x59, 0x49, 0xdd,
		0xc0, 0x55, 0xeb, 0x6d, 0xbe, 0x0a, 0x1e, 0xe2, 0x62, 0xce,
		0x53, 0x2e, 0xaa, 0xed, 0xe5, 0x7e, 0xf7, 0x1b, 0xbb, 0x09,
		0x75, 0x5e, 0x5f, 0xf9, 0x78, 0x12, 0x51, 0xa4, 0x63, 0x52,
		0xa4, 0xba, 0x45, 0xbc, 0x48, 0x89, 0xb2, 0x73, 0xb4, 0xa5,
		0x25, 0xd3, 0x1a, 0xd5, 0x9d, 0xff, 0x4e, 0xba, 0xd0, 0xb0,
		0xb5, 0x21, 0x11, 0x25, 0x4d, 0x84, 0x90, 0x6e, 0xcd, 0x68,
		0xd6, 0xd7, 0x39, 0xf7, 0x03, 0xb5, 0x7e, 0x78, 0x7e, 0x33,
		0x2c, 0x7f, 0x34, 0x8f, 0x6f, 0xb2, 0x24, 0xe0, 0x5f, 0xd6,
		0x18, 0x42, 0x4d, 0xb4, 0x5b, 0xe5, 0xc6, 0x92, 0xde, 0x54,
		0x37, 0x69, 0x36, 0x7d, 0xe0, 0x0b, 0xa2, 0x6a, 0xb7, 0x41,
		0xf4, 0x23, 0x09, 0x7f, 0x26, 0x64, 0xff, 0x10, 0x8a, 0x28,
		0x34, 0xca, 0x08, 0x81, 0xf5, 0x38, 0x58, 0x46, 0xd2, 0xc0,
		0x1c, 0x35, 0x31, 0x69, 0xcc, 0x4a, 0xed, 0x04, 0x22, 0x06,
		0xbf, 0x79, 0x62, 0x0e, 0x43, 0x5e, 0x90, 0xf3, 0x95, 0x6b,
		0x6e, 0xc3, 0x80, 0x9c, 0x63, 0xd1, 0xf7, 0xf2, 0x9f, 0x83,
		0xb2, 0x09, 0x08, 0xcf, 0xb3, 0x87, 0x79, 0xc6, 0x24, 0xe6,
		0x98, 0x58, 0xda, 0xdc, 0x0c, 0x67, 0x4c, 0x1f, 0xe7, 0xc3,
		0x26, 0xec, 0xdd, 0x7e, 0x91, 0xb0, 0x31, 0x99, 0x5c, 0x93,
		0x52, 0x17, 0xa2, 0x0f, 0xb1, 0xfb, 0x09, 0xd2, 0xa9, 0xe5,
		0xdf, 0x1e, 0x5c, 0xa8, 0xf5, 0x0c, 0x20, 0xc3, 0xe3, 0x07,
		0x32, 0x1b, 0x42, 0xc1, 0x58, 0xb2, 0x1c, 0x52, 0x7d, 0x56,
		0xf8, 0x0c, 0xad, 0x03, 0xf5, 0x40, 0x07, 0x9c, 0xf4, 0x41,
		0xf5, 0x54, 0xed, 0x66, 0x11, 0xd6, 0x98, 0xa4, 0x32, 0xd2,
		0x94, 0x02, 0x74, 0xa9, 0xe1, 0x3a, 0x61, 0x18, 0x37, 0x54,
		0xce, 0x03, 0x17, 0xc9, 0xc9, 0x99, 0x22, 0xd7, 0x3f, 0x71,
		0x7f, 0xf5, 0x8d, 0xa3,
	];

	const result: [_]u8 = [
		0x00, 0x01, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
		0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
		0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
		0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
		0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
		0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
		0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
		0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
		0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
		0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
		0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
		0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
		0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
		0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
		0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
		0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
		0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
		0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
		0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
		0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
		0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
		0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
		0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
		0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
		0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
		0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
		0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
		0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
		0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
		0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
		0x00, 0x30, 0x51, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86, 0x48,
		0x01, 0x65, 0x03, 0x04, 0x02, 0x03, 0x05, 0x00, 0x04, 0x40,
		0xcd, 0xcc, 0x9b, 0x65, 0xfe, 0xa7, 0xb2, 0x4d, 0xb6, 0xae,
		0xb7, 0xfb, 0xcb, 0x78, 0xb6, 0x10, 0x64, 0xcd, 0x9a, 0xdb,
		0x81, 0x4f, 0xc0, 0x8a, 0x17, 0xd3, 0xc0, 0x6b, 0xa2, 0xdd,
		0xbc, 0x61, 0xeb, 0xbf, 0xe2, 0x8e, 0x91, 0xf2, 0xd6, 0x75,
		0xa2, 0x5b, 0x0e, 0x61, 0x85, 0x74, 0xda, 0xcc, 0x94, 0x59,
		0xc4, 0x4b, 0x95, 0x0f, 0x80, 0xf6, 0x5e, 0xd4, 0x68, 0x09,
		0x65, 0x5f, 0x28, 0x85,
	];

	let xc = alloc(x);
	defer free(xc);

	pubexp(&sign3072.pub, xc, pubbuf)!;
	assert(bytes::equal(result, xc));
	privexp(&sign3072.priv, xc, privbuf)!;
	assert(bytes::equal(x, xc));
};
